//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO;
using System.IO.Packaging;
using System.Security.Permissions;
using System.Windows.Media.Imaging;
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Serialization;

namespace OpenLS.Spreadsheet
{
    static class PictureHelper
    {
        public static BitmapFrame ReadOptionalPicture(ReadContext c)
        {
            if (c.ReaderLocalName == "picture")
            {
                using (ReadContext picture = c.Read("picture"))
                {
                    string id = picture.GetString(OfficeConstants.RelationshipNamespace + "id");
                    PackageRelationship relationship = c.Part.GetRelationship(id);
                    PackagePart bitmapPart = c.Part.Package.GetPart(PackUriHelper.ResolvePartUri(c.Part.Uri, relationship.TargetUri));
                    return BitmapDecoder.Create(bitmapPart.GetStream(), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad).Frames[0];
                }
            }
            return null;

        }
        static bool HasUnmanagedCodePermision()
        {
            try
            {
                new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void WriteOptionalPicture(WriteContext c, BitmapSource Background)
        {
            if (Background == null)
                return;
            if (!HasUnmanagedCodePermision())
                return;
            using (WriteContext backgroundE = c.Write("picture"))
            {
                BitmapEncoder encoder = null;
                BitmapFrame f;
                if (Background is BitmapFrame)
                {
                    f = (BitmapFrame)Background;
                    if (f.Decoder != null)
                        encoder = BitmapEncoder.Create(f.Decoder.CodecInfo.ContainerFormat);
                }
                f = BitmapFrame.Create(Background);
                if (encoder == null)
                    encoder = OpenLS.Drawing.Internals.PixelFormatHelper.GetEncoder(f.Format);
                Uri u1 = c.GetAvailableUri("background{0}" + encoder.CodecInfo.FileExtensions.Split(',')[0]);
                PackageRelationship relationship = c.Part.CreateRelationship(
                    u1, TargetMode.Internal, SpreadsheetConstants.ImageRelationshipNamespace);
                Uri uu = PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri);
                PackagePart backgroundPart = c.Part.Package.CreatePart(uu, encoder.CodecInfo.MimeTypes.Split(',')[0]);

                encoder.Frames.Add(f);
                using (Stream dest = backgroundPart.GetStream())
                {
                    encoder.Save(dest);
                }
                backgroundE.SetString(OfficeConstants.RelationshipNamespace + "id", relationship.Id);

            }
        }

    }
}